package com.bobo.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bobo.rbac.domain.Menu;
import com.bobo.rbac.domain.RoleMenu;
import com.bobo.rbac.domain.User;
import com.bobo.rbac.domain.UserRole;
import com.bobo.rbac.service.MenuService;
import com.bobo.rbac.mapper.MenuMapper;
import com.bobo.rbac.service.RoleMenuService;
import com.bobo.rbac.service.UserRoleService;
import com.bobo.rbac.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author bobo
* @description 针对表【menu】的数据库操作Service实现
* @createDate 2022-07-27 14:42:55
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>
    implements MenuService{
    @Autowired
    UserService userService;
    @Autowired
    UserRoleService userRoleService;
    @Autowired
    RoleMenuService roleMenuService;
    @Autowired
    MenuMapper menuMapper;

    @Override
    public List<Menu> getTreeMenuByUsername(String username) {
        List<Menu> menuList =null;
        if(!username.equals("admin")) {
            //根据用户名获取user对象
            QueryWrapper<User> wrapper = new QueryWrapper<User>();
            wrapper.eq("username", username);
            User user = userService.getOne(wrapper);
            //根据用户id获取角色信息
            QueryWrapper<UserRole> wrapper2 = new QueryWrapper<UserRole>();
            wrapper2.eq("uid", user.getId());
            List<UserRole> userRoleList = userRoleService.list(wrapper2);
            //把UserRole 的角色id提取
            List<Integer> ridList = userRoleList.stream().map(ur -> ur.getRid()).collect(Collectors.toList());
            //根据角色id查询菜单对象
            QueryWrapper<RoleMenu> wrapper3 = new QueryWrapper<RoleMenu>();
            wrapper3.in("rid", ridList);
            List<RoleMenu> roleMenuList = roleMenuService.list(wrapper3);
            //提取出菜单id
            List<Integer> midList = roleMenuList.stream().map(rm -> rm.getMid()).collect(Collectors.toList());

            ///根据菜单id获取菜单集合对象
            QueryWrapper<Menu> wrapper4 = new QueryWrapper<Menu>();
            wrapper4.in("id", midList);
         menuList=  this.list(wrapper4);
        }else{
          menuList = this.list();//管理员查询所有菜单
        }

       //以下内容是封装菜单的父子关系
        //1 先找到父级菜单
       List<Menu> menuListByPid = getMenuListByPid(0,menuList);
       //2.封装父子关系
         getTreeMenu(menuListByPid,menuList);
        return menuListByPid;
    }

    @Override
    public List<Menu> listByPid(Integer pid) {
        return menuMapper.listByPid(pid);
    }

    /**
     * 使用递归获取树形菜单
     * @param menuListByPid
     * @param menuList
     */
    private void getTreeMenu(List<Menu> menuListByPid, List<Menu> menuList) {
        menuListByPid.forEach(pm->{
            List<Menu> list = getMenuListByPid(pm.getId(), menuList);
            if(null!=list){
                pm.setSubMenuList(list);
                getTreeMenu(list,menuList);
            }
        });

    }

    /**
     * 获取一级菜单
     * @param pid
     * @param menuList
     * @return
     */
    private List<Menu> getMenuListByPid(int pid, List<Menu> menuList) {
        List<Menu>  list = new ArrayList<>();
        menuList.forEach(m->{
            if(m.getParentid()==pid){
                list.add(m);
            }
        });
        return list;
    }
}




